package com.zhike.common.utils;

import io.netty.channel.Channel;
import io.netty.util.AttributeKey;

import java.util.UUID;

/**
 * 服务端session
 */
public class ServerSession {
    private static final AttributeKey<ServerSession> SESSION_KEY = AttributeKey.valueOf("SESSION_KEY");
    private String userId;
    private Channel channel;
    /**
     * 会话session，用户和DistributionSession绑定
     */
    private String sessionId = UUID.randomUUID().toString();

    public ServerSession(String userId, Channel channel) {
        this.userId = userId;
        this.channel = channel;
    }

    public String getUserId() {
        return userId;
    }

    public void setUser(String userId) {
        this.userId = userId;
    }

    public Channel getChannel() {
        return channel;
    }

    public void setChannel(Channel channel) {
        this.channel = channel;
    }

    public String getSessionId() {
        return sessionId;
    }

    public void setSessionId(String sessionId) {
        this.sessionId = sessionId;
    }

    public ServerSession bind() {
        this.channel.attr(SESSION_KEY).set(this);
        ServerSessionMap.add(this.getUserId(), this);
        return this;
    }
}
